//import { fileURLToPath, URL } from 'node:url'
import { resolve } from 'path';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite';
//npm install @vitejs/plugin-vue-jsx -D
import vueJsx from '@vitejs/plugin-vue-jsx';

// https://vitejs.dev/config/
const root = process.cwd();
function pathResolve(dir: string) {
  return resolve(root, '.', dir);
}

export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    AutoImport({
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    }),
    VueI18nPlugin({
      runtimeOnly: true,
      compositionOnly: true,
      include: [resolve(__dirname, 'src/locales/**')]
    })
  ],
  css: {
    preprocessorOptions: {
      scss: {
        javascriptEnabled: true,
        // 配置自己的全局变量
        additionalData: '@import "./src/styles/variable.scss";'
      }
    }
  },
  server: {
    port: 5173,
    proxy: {
      // 选项写法
      '/v1': {
        target: 'https://localhost:44368',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/v1/, '')
      }
    },
    hmr: {
      overlay: false
    },
    host: '0.0.0.0'
  },
  resolve: {
    // alias: {
    //   '@': fileURLToPath(new URL('./src', import.meta.url))
    // }
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css', '.scss'],
    alias: [
      {
        find: 'vue-i18n',
        replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
      },
      {
        find: '@',
        replacement: `${pathResolve('src')}/`
      }
    ]
  }
});
